//
//  SearchReplyResultViewController.m
//  pushNewsManager
//
//  Created by Jobs on 8/6/17.
//  Copyright © 2017 com.Iori. All rights reserved.
//

#import "SearchReplyResultViewController.h"
#import "Header.h"
#import "ApproveReplyTableViewCell.h"
#import "SetCMTClearApi.h"
#import "DeleteCMTApi.h"
#import "DeleteAllCMTApi.h"

@interface SearchReplyResultViewController ()

@end

@implementation SearchReplyResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.estimatedRowHeight = 44; // some constant value
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    
//    WS(weakself);
//    [self.tableView addPullToRefreshWithActionHandler:^{
//        //        [weakself loadNewsDataFromServerWithChannelID:weakself.currentChannelID completeBlock:^(BOOL finished){
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [weakself.tableView.pullToRefreshView stopAnimating];
//            [weakself.tableView reloadData];
//        });
//        
//        //        }];
//    }];
//    self.tableView.av_footer = [AVFooterRefresh createFooterWithRefreshingBlock:^{
//        //        [weakself addNewsDataFromServerWithChannelID:weakself.currentChannelID completeBlock:^(BOOL finished) {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [weakself.tableView.av_footer endFooterRefreshing];
//            [weakself.tableView reloadData];
//        });
//        //        }];
//    } scrollView:self.tableView];
//    self.tableView.av_footer.activityFooter.color = [UIColor whiteColor];
//    self.tableView.av_footer.footerLabel.textColor = [UIColor whiteColor];
//    self.tableView.av_footer.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//    
//    self.tableView.pullToRefreshView.textColor = [UIColor whiteColor];
//    self.tableView.pullToRefreshView.arrowColor = [UIColor whiteColor];
//    self.tableView.pullToRefreshView.activityIndicatorViewColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -table delegate-

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section != 0) return 0;
    return 34;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section != 0) return nil;
    UILabel *labTitle = [[UILabel alloc] init];
    labTitle.text = [@"    " stringByAppendingString:[NSString stringWithFormat:@"%li条评论搜索结果", self.dataSource.count]];
    labTitle.textColor = [UIColor whiteColor];
    return labTitle;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UILabel *lab = [[UILabel alloc] init];
    lab.textColor = [UIColor whiteColor];
    NSString *strTime = self.dataSource[section].art_show_time;
    if(strTime.length > 0)
    {
        lab.text = [@"    " stringByAppendingString:strTime];
    }
    else
    {
        lab.text = @"";
    }
    lab.font = [UIFont systemFontOfSize:13];
    return lab;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];;
    
    ApproveReplyTableViewCell *__cell = (ApproveReplyTableViewCell*)cell;
    __cell.parentTable = tableView;
    __cell.parentVC = self;
    //[__cell updateContent:nil];
    __cell.imageHead.layer.cornerRadius = 10;
    __cell.labName.text = self.dataSource[indexPath.section].member.member_nickname;
    [__cell.imageHead sd_setImageWithURL:[NSURL URLWithString:self.dataSource[indexPath.section].member.member_pic] placeholderImage:[UIImage imageNamed:@"nan1"]];
    __cell.labComment.text = self.dataSource[indexPath.section].cmt_content;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewAutomaticDimension;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(weakSelf);

    UITableViewRowAction *action_delete = [UITableViewRowAction
                                           rowActionWithStyle: UITableViewRowActionStyleDefault
                                           title: @"  删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
                                           {
                                               [tableView setEditing:NO animated:YES];
                                               DeleteCMTApi *api = [DeleteCMTApi new];
                                               api.admin_id = [UserInfo sharedUser].admin.admin_id;
                                               api.cmt_id = weakSelf.dataSource[indexPath.section].cmt_id;
                                               [MessageBox displayLoadingInView:weakSelf.view];
                                               [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                                                   [MessageBox removeLoading];
                                                   if([IoriJsonHelper getErrorCode:request] == 100)
                                                   {
                                                       NSMutableArray *tempDataSource = [NSMutableArray arrayWithArray:weakSelf.dataSource];
                                                       [tempDataSource removeObjectAtIndex:indexPath.section];
                                                       weakSelf.dataSource = tempDataSource;
                                                       [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
                                                   }
                                                   else
                                                   {
                                                       [weakSelf showMessage:[IoriJsonHelper getError:request]];
                                                   }
                                               } failure:^(YTKBaseRequest *request) {
                                                   [weakSelf showMessage:request.description];
                                                   [MessageBox removeLoading];
                                               }];
                                           }];
    UITableViewRowAction *action_state = [UITableViewRowAction
                                          rowActionWithStyle: UITableViewRowActionStyleNormal
                                          title: @" 清理" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
                                          {
                                              [tableView setEditing:NO animated:YES];
                                              SetCMTClearApi *api = [SetCMTClearApi new];
                                              api.admin_id = [UserInfo sharedUser].admin.admin_id;
                                              api.member_id = weakSelf.dataSource[indexPath.section].member_id;
                                              [MessageBox displayLoadingInView:weakSelf.view];
                                              [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                                                  [MessageBox removeLoading];
                                                  if([IoriJsonHelper getErrorCode:request] == 100)
                                                  {
                                                      [weakSelf showMessage:@"清理成功"];
                                                      [weakSelf.navigationController popViewControllerAnimated:YES];
                                                  }
                                                  else
                                                  {
                                                      [weakSelf showMessage:[IoriJsonHelper getError:request]];
                                                  }
                                              } failure:^(YTKBaseRequest *request) {
                                                  [weakSelf showMessage:request.description];
                                                  [MessageBox removeLoading];
                                              }];
                                          }];
    return @[action_delete , action_state];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
}

- (IBAction)btnDeleteAll_click:(UIButton *)sender
{
    WS(weakSelf);
    if(self.dataSource.count == 0) return;
    DeleteAllCMTApi *api = [DeleteAllCMTApi new];
    api.admin_id = [UserInfo sharedUser].admin.admin_id;
    NSMutableArray<NSString*> *arrayID = [NSMutableArray arrayWithCapacity:4];
    [self.dataSource enumerateObjectsUsingBlock:^(CMTEntity * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [arrayID addObject:obj.cmt_id];
    }];
    api.cmt_id = [arrayID componentsJoinedByString:@","];
    [MessageBox displayLoadingInView:weakSelf.view];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        [MessageBox removeLoading];
        if([IoriJsonHelper getErrorCode:request] == 100)
        {
            [weakSelf showMessage:@"删除评论成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [weakSelf showMessage:[IoriJsonHelper getError:request]];
        }
    } failure:^(YTKBaseRequest *request) {
        [weakSelf showMessage:request.responseString];
        [MessageBox removeLoading];
    }];
}
@end
